﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace aboutls
{
    static class Program 
    {
        static void Main(string [] args)
        {
            var cmd = @"F:\ConsoleApp2.exe";//24 csharp 面向对象 析构
            //var cmd = @"F:\test.exe";//
            var file = @"F:\output.txt";
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            DosCommandDo.Execute(cmd,file,0);
            //DosCommandDo.ExecuteOut(cmd,file,0);

            Console.WriteLine(111);
            Console.ReadLine();
        }
    }

    internal class DosCommandDo
    {
        public static void Execute(string dosCommand, string fileName, int milliseconds)
        {
            if (dosCommand != null && dosCommand != "")
            {
                //TODO judgement the fileName
                Process process = new Process();
                ProcessStartInfo startInfo = new ProcessStartInfo();
                //startInfo.FileName = "cmd.exe";
                //startInfo.Arguments = "/c " + dosCommand;
                startInfo.FileName = dosCommand;// "cmd.exe";
                startInfo.UseShellExecute = false;
                startInfo.Verb = "runas";
                startInfo.RedirectStandardInput = false;
                startInfo.RedirectStandardOutput = true;
                startInfo.CreateNoWindow = true;
                process.StartInfo = startInfo;

                try
                {
                    if (process.Start())
                    {
                        if (milliseconds == 0)
                            process.WaitForExit();
                        else
                            process.WaitForExit(milliseconds);

                        System.IO.StreamWriter sw = new System.IO.StreamWriter(fileName,
                            false, System.Text.Encoding.GetEncoding("gb2312"));
                        System.IO.StreamReader reader = process.StandardOutput;//截取输出流
                        string line = reader.ReadLine();//每次读取一行
                        sw.WriteLine(line);
                        sw.Flush();

                        while (!reader.EndOfStream)
                        {
                            line = reader.ReadLine();
                            sw.WriteLine(line);
                            sw.Flush();
                        }

                        if (reader != null)
                            reader.Close();
                        if (sw != null)
                            sw.Close();
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                finally
                {
                    if (process != null)
                        process.Close();
                }
            }
        }

        public static void ExecuteOut(string dosCommand, string fileName, int milliseconds)
        {
            if (dosCommand != null && dosCommand != "")
            {
                Process process = new Process();
                ProcessStartInfo startInfo = new ProcessStartInfo();
                startInfo.FileName = "cmd.exe";
                startInfo.Arguments = "/c " + dosCommand + " > " + fileName;
                startInfo.UseShellExecute = false;
                startInfo.RedirectStandardInput = false;
                startInfo.RedirectStandardOutput = true;
                startInfo.CreateNoWindow = true;
                process.StartInfo = startInfo;

                try
                {
                    if (process.Start())
                    {
                        if (milliseconds == 0)
                            process.WaitForExit();
                        else
                            process.WaitForExit(milliseconds);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                finally
                {
                    if (process != null)

                        process.Close();
                }
            }
        }
    }
}